package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Autowired
    private WmNewsMapper wmNewsMapper;

    @Override
    public ResponseResult channleList() {
        Page<WmChannel> page = new Page<>(1,100);
        page = wmChannelMapper.selectPage(page, null);
        List<WmChannel> records = page.getRecords();
        return ResponseResult.okResult(records);
    }

    @Override
    public ResponseResult wmChanneSave(WmChannel wmChannel) {
        if (wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        String name = wmChannel.getName();
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmChannel::getName,name);
        Integer count = wmChannelMapper.selectCount(wrapper);
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        wmChannel.setCreatedTime(new Date());
        wmChannelMapper.insert(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public PageResponseResult channleList2(ChannelDto dto) {
        if (dto==null){
            return new PageResponseResult(0,0,0);
        }
        Integer page = dto.getPage();
        Integer size = dto.getSize();
        page=page==null?1:page;
        size=size==null?10:size;
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(dto.getName()),WmChannel::getName,dto.getName());
        wrapper.orderByDesc(WmChannel::getCreatedTime);
        Page<WmChannel> pageinfo = new Page<>(page,size);
        Page<WmChannel> wmChannelPage = wmChannelMapper.selectPage(pageinfo, wrapper);
        Long total = wmChannelPage.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(page,size,total.intValue());
        pageResponseResult.setData(wmChannelPage.getRecords());

        return pageResponseResult;
    }

    @Override
    public ResponseResult wmChanneUpdate(WmChannel wmChannel) {
        if (wmChannel==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer id = wmChannel.getId();
        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNews::getChannelId,id);
        wrapper.eq(WmNews::getStatus,9);
        Integer count = wmNewsMapper.selectCount(wrapper);
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"当前频道包含新闻无法删除");
        }
        boolean b = this.updateById(wmChannel);

        return b?ResponseResult.okResult(AppHttpCodeEnum.SUCCESS):ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }

    @Override
    public ResponseResult del(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel wmChannel = wmChannelMapper.selectById(id);
        Boolean status = wmChannel.getStatus();
        if (status){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"启动中的频道不能删除");
        }
        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNews::getChannelId,wmChannel.getId());
        Integer count = wmNewsMapper.selectCount(wrapper);
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"频道关联了新闻，无法删除");
        }
        boolean b = this.removeById(id);
        return b?ResponseResult.okResult(AppHttpCodeEnum.SUCCESS):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }


}